﻿using System;
using System.Collections.Generic;
using System.ComponentModel;

using JetBrains.Annotations;

using Rsdn.SmartApp;

namespace Rsdn.Janus
{
    public static class CommandHelper
    {
        /// <summary>
        /// Временно.
        /// </summary>
        public static void TryExecuteCommand(
            this IServiceProvider provider,
            [Localizable(false)] string commandName,
            IDictionary<string, object> commandParameters)
        {
            ICommandHandlerService commandHandlerService = provider.GetService<ICommandHandlerService>();
            CommandContext commandContext = new CommandContext(provider, commandParameters);
            CommandStatus commandStatus = commandHandlerService.QueryStatus(commandName, commandContext);

            if (commandHandlerService != null && commandStatus == CommandStatus.Normal)
            {
                commandHandlerService.ExecuteCommand(commandName, commandContext);
            }
        }

        /// <exception cref="ArgumentNullException"><paramref name="provider" /> is <c>null</c>.</exception>
        public static void ExecuteDefaultCommand([NotNull] this IServiceProvider provider)
        {
            if (provider == null)
            {
                throw new ArgumentNullException("provider");          //MLHIDE
            }

            IDefaultCommandService defaultCommandService = provider.GetRequiredService<IDefaultCommandService>();
            if (defaultCommandService.CommandName == null)
            {
                return;
            }

            ICommandHandlerService commandSvc = provider.GetRequiredService<ICommandHandlerService>();
            CommandContext context = new CommandContext(provider, defaultCommandService.Parameters);
            if (commandSvc.QueryStatus(defaultCommandService.CommandName, context) == CommandStatus.Normal)
            {
                commandSvc.ExecuteCommand(defaultCommandService.CommandName, context);
            }
        }
    }
}